
require_dependency 'jsmin'
require 'action_view/helpers/asset_tag_helper'

module JsminHelper

  

  #**
  #* javascriptファイルの圧縮とファイル結合スクリプトへパラメータを渡す
  #*
  def jscomp(paths=null)
    if !@jscomp_src
      @jscomp_src = Array.new
    end
    @jscomp_src.push(paths)
  end

  def csscomp(paths, prefix='')
    if !@csscomp_src
      @csscomp_src = Array.new
    end
    @csscomp_src.push(paths)
  end

  def jscomp_write()
    a = @jscomp_src.reverse
    paths = a.join(',')
    src = _compress(paths)
    return src
  end

  def csscomp_write()
    a = @csscomp_src.reverse
    paths = a.join(',')
    src = _compress(paths, 'css')
    return src
  end

  def _compress(paths, type='js')
    src = ''
    paths = paths.split(/\s*,\s*/)
    logger.debug paths.inspect

    is_development = (!RAILS_ENV.nil? && RAILS_ENV == 'development') ? true : false
    #is_development = true
    logger.debug is_development

    if is_development
      paths.each { |p| 
        if type == 'css'
          src += stylesheet_link_tag(p.strip) + "\n"
        else
          src += javascript_include_tag(p.strip) + "\n"
        end
      }
    else
      path = _file_compress(paths, type)
      if type == 'css'
        src += stylesheet_link_tag(path)
      else
        src += javascript_include_tag(path)
      end
    end

    return src
  end

  def _file_compress(path, type)
    ds = '/'
    public_dir = RAILS_ROOT + '/public'
    cache_dir_name = 'comp'

    if type == 'css'
      src_parent = 'stylesheets'
      ext = 'css'
      # cssは画像読み込みパスの問題もあって，jsよりも一階層下のディレクトリに配置
      cache_path = cache_dir_name# + "/cache"
    else
      src_parent = 'javascripts'
      ext = 'js'
      cache_path = cache_dir_name
    end
    cache_dir = public_dir + ds + src_parent + ds + cache_path.sub('/', ds)


    file_name = controller.controller_name + '_' + controller.action_name + '.' + ext
    file_name = file_name.downcase
    file_path = cache_dir + ds + file_name
    file_path_tmp = cache_dir + ds + 'tmp_' + file_name

    files = Array.new
    last_modified = 0

    path.each { |p| 
      p = p.strip
      _file_path = src_parent + ds + p + '.' + ext
      _real_path = public_dir + ds + _file_path

      files.push(_real_path)
      _last_modified = File::mtime(_real_path)

      if _last_modified.to_s > last_modified.to_s
        last_modified = _last_modified
      else
        last_modified = last_modified
      end

    }

    is_compress = true
    if FileTest.file?(file_path)
      modefied = File::mtime(file_path)
      if last_modified <= modefied
        is_compress = false
      end
    end

    content = ''
    if is_compress
      files.each { |file| 
        file = open(file)
        content += file.read + "\n\n"
        file.close
      }

      fp = File.open(file_path_tmp, "w")
      if fp
        fp.puts content
        fp.close
      end

      if ext == 'js'
        JsMin.new(file_path_tmp, file_path).minify
      else
        file_name = 'tmp_' + file_name
      end

    end

    url_path = cache_path + '/' + file_name
    file_path = url_path.sub('.' + ext, '')
    return file_path
  end

end